﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	public class FanCoilSupportLoadCalculate
	{
		public FanCoilSupportLoadCalculate(Autodesk.Revit.DB.Document doc, FrmLayFanCoilLoadCalculate frm)
		{
			this.doc = doc;
			this.cm = new ConnectHelper();
			this.sfdr = new SpaceFamilyDataReader();
			this.infoList = this.sfdr.GetAllEquipmentCoilInfo(doc);
			this.frm = frm;
		}

		public void Excute()
		{
			List<ElementId> spaceIds = this.GetSpaceIds();
            foreach (KeyValuePair<Autodesk.Revit.DB.View, List<Element>> keyValuePair in this.GetSpaces())
			{
                Autodesk.Revit.DB.View key = keyValuePair.Key;
				List<Element> value = keyValuePair.Value;
				XYZ buildingCenterPoint = this.GetBuildingCenterPoint(key);
				if (buildingCenterPoint != null)
				{
					this.LayFanCoil(this.doc, key, buildingCenterPoint, value, ref spaceIds);
				}
			}
		}

		public Dictionary<Autodesk.Revit.DB.View, List<Element>> GetSpaces()
		{
			Dictionary<Autodesk.Revit.DB.View, List<Element>> result = new Dictionary<Autodesk.Revit.DB.View, List<Element>>();
			switch (this.frm.fanCoilLayType)
			{
			case FanCoilLayType.CurrentProject:
				result = this.GetCurrentProjectSpaces();
				break;
			case FanCoilLayType.CurrentFloor:
				result = this.GetCurrentFloorSpaces();
				break;
			case FanCoilLayType.FrameSelection:
				result = this.GetFrameSelectionSpaces();
				break;
			}
			return result;
		}

		public Dictionary<Autodesk.Revit.DB.View, List<Element>> GetCurrentProjectSpaces()
		{
			List<Autodesk.Revit.DB.View> floorPlanViews = this.GetFloorPlanViews(this.doc);
			ElementCategoryFilter elementCategoryFilter = new ElementCategoryFilter(BuiltInCategory.OST_MEPSpaces);
			Dictionary<Autodesk.Revit.DB.View, List<Element>> dictionary = new Dictionary<Autodesk.Revit.DB.View, List<Element>>();
			foreach (Autodesk.Revit.DB.View view in floorPlanViews)
			{
				ICollection<Element> source = new FilteredElementCollector(this.doc, view.Id).WherePasses(elementCategoryFilter).WhereElementIsNotElementType().ToElements();
				dictionary.Add(view, source.ToList<Element>());
			}
			return dictionary;
		}

		public List<Autodesk.Revit.DB.View> GetFloorPlanViews(Autodesk.Revit.DB.Document doc)
		{
			ViewFamilyType floorPlanViewFamilyType = this.GetFloorPlanViewFamilyType(doc);
			List<Autodesk.Revit.DB.View> list = new List<Autodesk.Revit.DB.View>();
			IList<Element> list2 = new FilteredElementCollector(doc).OfClass(typeof(Autodesk.Revit.DB.View)).ToElements();
			if (list2 != null)
			{
				foreach (Element element in list2)
				{
                    Autodesk.Revit.DB.View view = element as Autodesk.Revit.DB.View;
					if (view.GetTypeId() == floorPlanViewFamilyType.Id)
					{
						list.Add(view);
					}
				}
			}
			return list;
		}

		public ViewFamilyType GetFloorPlanViewFamilyType(Autodesk.Revit.DB.Document doc)
		{
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
			filteredElementCollector.OfClass(typeof(ViewFamilyType));
            return filteredElementCollector.Cast<ViewFamilyType>().First((ViewFamilyType vft) => (int)vft.ViewFamily == 109);
		}

		private List<ElementId> GetSpaceIds()
		{
			ElementCategoryFilter elementCategoryFilter = new ElementCategoryFilter(BuiltInCategory.OST_MEPSpaces);
			return new FilteredElementCollector(this.doc).WherePasses(elementCategoryFilter).WhereElementIsNotElementType().ToElementIds().ToList<ElementId>();
		}

		private Dictionary<Autodesk.Revit.DB.View, List<Element>> GetCurrentFloorSpaces()
		{
			Dictionary<Autodesk.Revit.DB.View, List<Element>> dictionary = new Dictionary<Autodesk.Revit.DB.View, List<Element>>();
			ElementCategoryFilter elementCategoryFilter = new ElementCategoryFilter(BuiltInCategory.OST_MEPSpaces);
			ICollection<Element> source = new FilteredElementCollector(this.doc, this.doc.ActiveView.Id).WherePasses(elementCategoryFilter).WhereElementIsNotElementType().ToElements();
			dictionary.Add(this.doc.ActiveView, source.ToList<Element>());
			return dictionary;
		}

		private Dictionary<Autodesk.Revit.DB.View, List<Element>> GetFrameSelectionSpaces()
		{
			Dictionary<Autodesk.Revit.DB.View, List<Element>> dictionary = new Dictionary<Autodesk.Revit.DB.View, List<Element>>();
			ICollection<Element> source = new UIDocument(this.doc).Selection.PickElementsByRectangle(new HYSpaceFilter(), "请框选需要自动布置的空间...");
			dictionary.Add(this.doc.ActiveView, source.ToList<Element>());
			return dictionary;
		}

		private FamilyInstance LayFanCoilInSpace(XYZ locationPoint, FamilySymbol fs, Level level)
		{
			FamilyInstance result = null;
			if (fs == null)
			{
				return result;
			}
			return this.doc.Create.NewFamilyInstance(locationPoint, fs, level, 0);
		}

		private EquipmentCoilParameter GetFanCoilParameter(Space space, List<Level> levels, out Level level, out double offset)
		{
			EquipmentCoilParameter equipmentCoilParameter = new EquipmentCoilParameter();
			level = null;
			offset = 0.0;
			Parameter parameter = space.GetParameters("风机盘管型号").FirstOrDefault<Parameter>();
			if (parameter != null && parameter.HasValue)
			{
				equipmentCoilParameter.Name = parameter.AsString();
			}
			Parameter parameter2 = space.GetParameters("风机盘管结构形式").FirstOrDefault<Parameter>();
			if (parameter2 != null && parameter2.HasValue)
			{
				bool flag = false;
				equipmentCoilParameter.StructType = parameter2.AsString();
				level = ((parameter2.AsString() == "立式") ? space.Level : this.GetUpLimit(levels, space, ref flag));
                offset = ((parameter2.AsString() == "立式") ? space.GetParameter(BuiltInParameter.ROOM_LOWER_OFFSET).AsDouble() : (flag ? (space.GetParameter((BuiltInParameter)(-1006924)).AsDouble() - 1.6404199475065617) : -1.6404199475065617));
			}
			Parameter parameter3 = space.GetParameters("风机盘管安装形式").FirstOrDefault<Parameter>();
			if (parameter3 != null && parameter2.HasValue)
			{
				equipmentCoilParameter.InstallType = parameter3.AsString();
			}
			Parameter parameter4 = space.GetParameters("风机盘管厂家").FirstOrDefault<Parameter>();
			if (parameter4 != null && parameter4.HasValue)
			{
				equipmentCoilParameter.Factory = parameter4.AsString();
			}
			return equipmentCoilParameter;
		}

		private Level GetUpLimit(List<Level> levels, Space space, ref bool spaceUpLimt)
		{
			int num = levels.FindIndex((Level p) => p.Id.IntegerValue == space.Level.Id.IntegerValue);
			Level result;
			if (num + 1 < levels.Count)
			{
				result = levels.ElementAt(num + 1);
				spaceUpLimt = false;
			}
			else
			{
				result = space.Level;
				spaceUpLimt = true;
			}
			return result;
		}

		private void SetFanCoilParams(Space space, FamilyInstance fi, double offset)
		{
			if (fi == null)
			{
				return;
			}
			Parameter parameter = fi.GetParameter(BuiltInParameter.INSTANCE_FREE_HOST_OFFSET_PARAM);
			if (parameter != null && !parameter.IsReadOnly)
			{
				parameter.Set(offset);
			}
			Parameter parameter2 = space.GetParameters("风机盘管厂家").FirstOrDefault<Parameter>();
			if (parameter2 != null && parameter2.HasValue)
			{
				Parameter parameter3 = fi.GetParameters("风机盘管厂家").FirstOrDefault<Parameter>();
				if (parameter3 != null && !parameter3.IsReadOnly)
				{
					parameter3.Set(parameter2.AsString());
				}
			}
			Parameter parameter4 = space.GetParameters("风机盘管结构形式").FirstOrDefault<Parameter>();
			if (parameter4 != null && parameter4.HasValue)
			{
				Parameter parameter5 = fi.GetParameters("风机盘管结构形式").FirstOrDefault<Parameter>();
				if (parameter5 != null && !parameter5.IsReadOnly)
				{
					parameter5.Set(parameter4.AsString());
				}
			}
			Parameter parameter6 = space.GetParameters("风机盘管安装形式").FirstOrDefault<Parameter>();
			if (parameter6 != null && parameter6.HasValue)
			{
				Parameter parameter7 = fi.GetParameters("风机盘管安装形式").FirstOrDefault<Parameter>();
				if (parameter7 != null && !parameter7.IsReadOnly)
				{
					parameter7.Set(parameter6.AsString());
				}
			}
			Parameter parameter8 = space.GetParameters("风机盘管型号").FirstOrDefault<Parameter>();
			if (parameter8 != null && parameter8.HasValue)
			{
				Parameter parameter9 = fi.GetParameters("风机盘管型号").FirstOrDefault<Parameter>();
				if (parameter9 != null && !parameter9.IsReadOnly)
				{
					parameter9.Set(parameter8.AsString());
				}
			}
			Parameter parameter10 = space.GetParameter(BuiltInParameter.ROOM_DESIGN_SUPPLY_AIRFLOW_PARAM);
			Parameter parameter11 = space.GetParameters("风机盘管台数").FirstOrDefault<Parameter>();
			if (parameter10 != null && parameter10.HasValue && parameter11 != null && parameter11.HasValue && parameter11.AsInteger() != 0)
			{
				Parameter parameter12 = fi.GetParameters("风量").FirstOrDefault<Parameter>();
				if (parameter12 != null && !parameter12.IsReadOnly)
				{
					parameter12.Set(parameter10.AsDouble() / (double)parameter11.AsInteger());
				}
			}
			Parameter parameter13 = space.GetParameter(BuiltInParameter.ROOM_DESIGN_COOLING_LOAD_PARAM);
			if (parameter13 != null && parameter13.HasValue && parameter11 != null && parameter11.HasValue && parameter11.AsInteger() != 0)
			{
				Parameter parameter14 = fi.GetParameters("制冷量").FirstOrDefault<Parameter>();
				if (parameter14 != null && !parameter14.IsReadOnly)
				{
					parameter14.Set(parameter13.AsDouble() / (double)parameter11.AsInteger());
				}
			}
			Parameter parameter15 = space.get_Parameter(BuiltInParameter.ROOM_DESIGN_HEATING_LOAD_PARAM);
			if (parameter15 != null && parameter15.HasValue && parameter11 != null && parameter11.HasValue && parameter11.AsInteger() != 0)
			{
				Parameter parameter16 = fi.GetParameters("制热量").FirstOrDefault<Parameter>();
				if (parameter16 != null && !parameter16.IsReadOnly)
				{
					parameter16.Set(parameter15.AsDouble() / (double)parameter11.AsInteger());
				}
			}
		}

		private FamilySymbol GetFamilySymbol(EquipmentCoilParameter ecp)
		{
			FamilySymbol result = null;
			EquipmentCoilParameter equipmentCoilParameter = this.infoList.FirstOrDefault((EquipmentCoilParameter p) => p.Name == ecp.Name && p.StructType == ecp.StructType && p.InstallType == ecp.InstallType && p.Factory == ecp.Factory);
			if (equipmentCoilParameter != null)
			{
				try
				{
					result = this.sfdr.GetFamilySymbolById(Convert.ToInt32(equipmentCoilParameter.Id), "EquipmentCoil", this.doc, BuiltInCategory.OST_MechanicalEquipment);
				}
				catch (Exception ex)
				{
					ex.ToString();
					MessageBox.Show("读取族库失败", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				}
			}
			return result;
		}

		private XYZ GetCentralPoint(List<Element> speces)
		{
			int count = speces.Count;
			double num = 0.0;
			double num2 = 0.0;
			if (count < 1)
			{
				return null;
			}
			foreach (Element element in speces)
			{
				XYZ point = ((element as Space).Location as LocationPoint).Point;
				num += point.X;
				num2 += point.Y;
			}
			return new XYZ(num / (double)count, num2 / (double)count, 0.0);
		}

        private FamilyInstance LayFanCoil(Autodesk.Revit.DB.Document doc, Autodesk.Revit.DB.View view, XYZ centralPoint, List<Element> spaces, ref List<ElementId> spaceIds)
		{
			List<Level> projectLevels = this.GetProjectLevels();
			List<FamilyInstance> doors = this.GetDoors();
			foreach (Element element in spaces)
			{
				try
				{
					Space space = element as Space;
					if (space != null)
					{
						if (!(spaceIds.FirstOrDefault((ElementId p) => p.IntegerValue == space.Id.IntegerValue) == null))
						{
							spaceIds.Remove(space.Id);
							FamilyInstance layDoor = null;
							bool flag = false;
							XYZ layPoint = this.GetLayPoint(view, centralPoint, space, doors, ref layDoor, ref flag);
							if (layPoint != null)
							{
								Level level = null;
								double offset = 0.0;
								FamilySymbol familySymbol = this.GetFamilySymbol(this.GetFanCoilParameter(space, projectLevels, out level, out offset));
								if (familySymbol != null)
								{
									this.transaction = new Transaction(doc, "风盘负荷计算布置风盘");
									this.transaction.Start();
									familySymbol.Activate();
									FamilyInstance fi = null;
									using (SubTransaction subTransaction = new SubTransaction(doc))
									{
										subTransaction.Start();
										fi = this.LayFanCoilInSpace(layPoint, familySymbol, level);
										this.SetFanCoilParams(space, fi, offset);
										subTransaction.Commit();
									}
									doc.Regenerate();
									if (flag)
									{
										this.MovedFan(view, layPoint, space, fi, layDoor);
									}
									this.transaction.Commit();
									doc.Regenerate();
								}
							}
						}
					}
				}
				catch (Autodesk.Revit.Exceptions.OperationCanceledException ex)
				{
					ex.ToString();
					break;
				}
				catch (Exception ex2)
				{
					ex2.ToString();
				}
				finally
				{
                    if (this.transaction != null && ((int)this.transaction.GetStatus() == 1 || (int)this.transaction.GetStatus() == 5))
					{
						this.transaction.RollBack();
					}
				}
			}
			return null;
		}

		private XYZ GetLayPoint(Autodesk.Revit.DB.View view, XYZ centralPoint, Space space, List<FamilyInstance> doors, ref FamilyInstance layDoor, ref bool move)
		{
			XYZ result = null;
			Parameter parameter = space.GetParameters("风机盘管结构形式").FirstOrDefault<Parameter>();
			if (parameter != null && parameter.HasValue)
			{
				if (parameter.AsString() == "立式")
				{
					result = this.GetLocationPoint(view, centralPoint, space);
					move = true;
				}
				else if (parameter.AsString() == "卧式")
				{
					result = this.GetHorizontalFanLocationPoint(space, doors, ref layDoor, ref move);
				}
				else
				{
					result = (space.Location as LocationPoint).Point;
					move = false;
				}
			}
			return result;
		}

		private XYZ GetLocationPoint(Autodesk.Revit.DB.View view, XYZ centralPoint, Space space)
		{
			XYZ xyz = null;
			XYZ point = (space.Location as LocationPoint).Point;
			XYZ xyz2 = (point - centralPoint).Normalize();
			bool flag = false;
			IList<IList<BoundarySegment>> boundarySegments = space.GetBoundarySegments(new SpatialElementBoundaryOptions());
			if (boundarySegments != null)
			{
				foreach (IList<BoundarySegment> list in boundarySegments)
				{
					foreach (BoundarySegment segment in list)
					{
						XYZ endPoint = UnifiedModified.GetBoundarySegmentCurve(segment).GetEndPoint(0);
						XYZ xyz3 = (endPoint - point).Normalize();
						if (xyz3.X * xyz2.X > 0.0 && xyz3.Y * xyz2.Y > 0.0)
						{
							xyz = endPoint;
							flag = true;
							break;
						}
						XYZ endPoint2 = UnifiedModified.GetBoundarySegmentCurve(segment).GetEndPoint(0);
						XYZ xyz4 = (endPoint2 - point).Normalize();
						if (xyz4.X * xyz2.X > 0.0 && xyz4.Y * xyz2.Y > 0.0)
						{
							xyz = endPoint2;
							flag = true;
							break;
						}
						if (xyz4.X * xyz2.X == 0.0 || xyz4.Y * xyz2.Y == 0.0)
						{
							xyz = endPoint;
							flag = true;
							break;
						}
					}
					if (flag)
					{
						break;
					}
				}
			}
			if (xyz == null)
			{
				xyz = this.GetLocationPoint(view, this.SpaceCenter(space), space);
			}
			return xyz;
		}

		private XYZ SpaceCenter(Space space)
		{
			new XYZ();
			List<XYZ> list = new List<XYZ>();
			IList<IList<BoundarySegment>> boundarySegments = space.GetBoundarySegments(new SpatialElementBoundaryOptions());
			if (boundarySegments != null)
			{
				foreach (IList<BoundarySegment> list2 in boundarySegments)
				{
					foreach (BoundarySegment boundarySegment in list2)
					{
						XYZ endPoint = boundarySegment.GetCurve().GetEndPoint(0);
						if (!list.Contains(endPoint))
						{
							list.Add(endPoint);
						}
						XYZ endPoint2 = boundarySegment.GetCurve().GetEndPoint(1);
						if (!list.Contains(endPoint2))
						{
							list.Add(endPoint2);
						}
					}
				}
			}
			double num = 0.0;
			double num2 = 0.0;
			if (list.Count == 0)
			{
				return null;
			}
			foreach (XYZ xyz in list)
			{
				num += xyz.X;
				num2 += xyz.Y;
			}
			return new XYZ(num / (double)list.Count, num2 / (double)list.Count, 0.0);
		}

		private void MoveVerticalFan(XYZ layPoint, Space space, FamilyInstance fi)
		{
			XYZ point = (space.Location as LocationPoint).Point;
			double num = (point.X - layPoint.X != 0.0) ? ((point.X - layPoint.X) / Math.Abs(point.X - layPoint.X)) : 0.0;
			double num2 = (point.Y - layPoint.Y != 0.0) ? ((point.Y - layPoint.Y) / Math.Abs(point.Y - layPoint.Y)) : 0.0;
			double num3 = 0.0;
			double num4 = 0.0;
			Parameter parameter = fi.GetParameters("长度").FirstOrDefault<Parameter>();
			if (parameter != null && parameter.HasValue)
			{
				num3 = parameter.AsDouble();
			}
			Parameter parameter2 = fi.GetParameters("宽度").FirstOrDefault<Parameter>();
			if (parameter2 != null && parameter2.HasValue)
			{
				num4 = parameter2.AsDouble();
			}
			if (num2 > 0.0)
			{
				Line line = Line.CreateBound(layPoint, layPoint + new XYZ(0.0, 0.0, 1.0));
				ElementTransformUtils.RotateElement(this.doc, fi.Id, line, Math.PI);
			}
			if (num < 0.0 && num2 < 0.0)
			{
				ElementTransformUtils.MoveElement(this.doc, fi.Id, new XYZ(-num3, -num4, 0.0));
				return;
			}
			if (num > 0.0 && num2 < 0.0)
			{
				ElementTransformUtils.MoveElement(this.doc, fi.Id, new XYZ(0.0, -num4, 0.0));
				return;
			}
			if (num > 0.0 && num2 > 0.0)
			{
				ElementTransformUtils.MoveElement(this.doc, fi.Id, new XYZ(num3, num4, 0.0));
				return;
			}
			if (num < 0.0 && num2 > 0.0)
			{
				ElementTransformUtils.MoveElement(this.doc, fi.Id, new XYZ(0.0, num4, 0.0));
			}
		}

		private XYZ GetBuildingCenterPoint(Autodesk.Revit.DB.View view)
		{
			ElementCategoryFilter elementCategoryFilter = new ElementCategoryFilter(BuiltInCategory.OST_Walls);
			List<Element> list = new FilteredElementCollector(this.doc, view.Id).WherePasses(elementCategoryFilter).WhereElementIsNotElementType().ToElements().ToList<Element>();
			list.AddRange(this.GetLinkedWalls());
			List<XYZ> list2 = new List<XYZ>();
			foreach (Element element in list)
			{
				Wall wall = element as Wall;
				if (wall != null)
				{
					LocationCurve locationCurve = wall.Location as LocationCurve;
					XYZ item = (locationCurve.Curve.GetEndPoint(0) + locationCurve.Curve.GetEndPoint(0)) / 2.0;
					list2.Add(item);
				}
			}
			double num = 0.0;
			double num2 = 0.0;
			if (list2.Count == 0)
			{
				return null;
			}
			foreach (XYZ xyz in list2)
			{
				num += xyz.X;
				num2 += xyz.Y;
			}
			return new XYZ(num / (double)list2.Count, num2 / (double)list2.Count, 0.0);
		}

		private List<Element> GetLinkedWalls()
		{
			List<Element> list = new List<Element>();
			List<Element> list2 = new FilteredElementCollector(this.doc, this.doc.ActiveView.Id).OfCategory(BuiltInCategory.OST_RvtLinks).WhereElementIsNotElementType().ToElements().ToList<Element>();
			if (list2.Count > 0)
			{
				List<Element> list3 = new List<Element>();
				foreach (Element element in list2)
				{
					list3.Clear();
					FilteredElementCollector filteredElementCollector = new FilteredElementCollector((element as RevitLinkInstance).GetLinkDocument());
					ElementCategoryFilter elementCategoryFilter = new ElementCategoryFilter(BuiltInCategory.OST_Walls);
					list.AddRange(filteredElementCollector.WherePasses(elementCategoryFilter).WhereElementIsNotElementType().ToElements());
				}
			}
			return list;
		}

		private List<Level> GetProjectLevels()
		{
			List<Level> list = new List<Level>();
			ElementCategoryFilter elementCategoryFilter = new ElementCategoryFilter(BuiltInCategory.OST_Levels);
			foreach (Element element in new FilteredElementCollector(this.doc).WherePasses(elementCategoryFilter).WhereElementIsNotElementType().ToElements())
			{
				Level level = element as Level;
				if (level != null)
				{
					list.Add(level);
				}
			}
			return (from p in list
			orderby p.Elevation
			select p).ToList<Level>();
		}

		private XYZ GetHorizontalFanLocationPoint(Space space, List<FamilyInstance> doors, ref FamilyInstance layDoor, ref bool move)
		{
			Room room = space.Room;
			XYZ point;
			if (room != null)
			{
				FamilyInstance familyInstance = doors.FirstOrDefault((FamilyInstance p) => (p.Room != null && p.Room.Id.IntegerValue == room.Id.IntegerValue) || (p.ToRoom != null && p.ToRoom.Id.IntegerValue == room.Id.IntegerValue) || (p.FromRoom != null && p.FromRoom.Id.IntegerValue == room.Id.IntegerValue));
				if (familyInstance != null)
				{
					layDoor = familyInstance;
					point = (familyInstance.Location as LocationPoint).Point;
					move = true;
				}
				else
				{
					point = (space.Location as LocationPoint).Point;
					move = false;
				}
			}
			else
			{
				List<FamilyInstance> list = new List<FamilyInstance>();
				List<Element> sapceElements = YJKRevitTools.GetSapceElements(space, BuiltInCategory.OST_Doors);
				if (sapceElements != null && sapceElements.Count > 0)
				{
					list.AddRange(sapceElements.Cast<FamilyInstance>().ToList<FamilyInstance>());
				}
				if (list == null || list.Count == 0)
				{
					point = (space.Location as LocationPoint).Point;
					move = false;
				}
				else
				{
					layDoor = list.FirstOrDefault<FamilyInstance>();
					point = (list.FirstOrDefault<FamilyInstance>().Location as LocationPoint).Point;
					move = true;
				}
			}
			return point;
		}

		private List<FamilyInstance> GetDoors()
		{
			List<FamilyInstance> list = new List<FamilyInstance>();
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.doc);
			ElementCategoryFilter elementCategoryFilter = new ElementCategoryFilter(BuiltInCategory.OST_Doors);
			foreach (Element element in filteredElementCollector.WherePasses(elementCategoryFilter).WhereElementIsNotElementType().ToElements())
			{
				FamilyInstance familyInstance = element as FamilyInstance;
				if (familyInstance != null && (familyInstance.Room != null || familyInstance.ToRoom != null || familyInstance.FromRoom != null))
				{
					list.Add(familyInstance);
				}
			}
			return list;
		}

		private void MovedFan(Autodesk.Revit.DB.View view, XYZ layPoint, Space space, FamilyInstance fi, FamilyInstance layDoor)
		{
			Parameter parameter = space.GetParameters("风机盘管结构形式").FirstOrDefault<Parameter>();
			if (parameter != null && parameter.HasValue)
			{
				if (parameter.AsString() == "立式")
				{
					this.MoveVerticalFan(layPoint, space, fi);
					return;
				}
				if (parameter.AsString() == "卧式")
				{
					this.MovedHorizontalFan(view, layPoint, space, fi, layDoor);
				}
			}
		}

		private void MovedHorizontalFan(Autodesk.Revit.DB.View view, XYZ layPoint, Space space, FamilyInstance fi, FamilyInstance layDoor)
		{
			Wall wall = layDoor.Host as Wall;
			if (wall != null)
			{
				double width = wall.Width;
				Line line = (wall.Location as LocationCurve).Curve as Line;
				if (line != null)
				{
					Curve curve = Line.CreateUnbound(line.GetEndPoint(0), line.Direction);
					XYZ point = (space.Location as LocationPoint).Point;
					XYZ xyzpoint = curve.Project(point).XYZPoint;
					XYZ xyz = new XYZ(point.X, point.Y, 0.0).Subtract(new XYZ(xyzpoint.X, xyzpoint.Y, 0.0)).Normalize();
					double num = 0.0;
					double num2 = 0.0;
					Parameter parameter = fi.GetParameters("长度").FirstOrDefault<Parameter>();
					if (parameter != null && parameter.HasValue)
					{
						num = parameter.AsDouble();
					}
					Parameter parameter2 = fi.GetParameters("宽度").FirstOrDefault<Parameter>();
					if (parameter2 != null && parameter2.HasValue)
					{
						num2 = parameter2.AsDouble();
					}
					Line line2 = Line.CreateBound(layPoint, layPoint + new XYZ(0.0, 0.0, 1.0));
					double num3 = 0.65616797900262469;
					if (xyz.IsAlmostEqualTo(view.RightDirection))
					{
						ElementTransformUtils.RotateElement(this.doc, fi.Id, line2, Math.PI*.5);
						ElementTransformUtils.MoveElement(this.doc, fi.Id, new XYZ(num2 + width / 2.0 + num3, -num / 2.0, 0.0));
						return;
					}
					if (xyz.IsAlmostEqualTo(-view.RightDirection))
					{
						ElementTransformUtils.RotateElement(this.doc, fi.Id, line2, -Math.PI*.5);
						ElementTransformUtils.MoveElement(this.doc, fi.Id, new XYZ(-num2 - width / 2.0 - num3, num / 2.0, 0.0));
						return;
					}
					if (xyz.IsAlmostEqualTo(view.UpDirection))
					{
						ElementTransformUtils.RotateElement(this.doc, fi.Id, line2, Math.PI);
						ElementTransformUtils.MoveElement(this.doc, fi.Id, new XYZ(num / 2.0, num2 + width / 2.0 + num3, 0.0));
						return;
					}
					if (xyz.IsAlmostEqualTo(-view.UpDirection))
					{
						ElementTransformUtils.MoveElement(this.doc, fi.Id, new XYZ(-num / 2.0, -num2 - width / 2.0 - num3, 0.0));
					}
				}
			}
		}

		private Document doc;

		private ConnectHelper cm;

		private Transaction transaction;

		private SpaceFamilyDataReader sfdr;

		private List<EquipmentCoilParameter> infoList;

		private FrmLayFanCoilLoadCalculate frm;
	}
}
